package experiment;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Hashtable;

import vo.User;
import JDBC.DatabaseDao;
import dao.StatusDao;
import dao.UserDao;

public class Preprocess {
	// 计算哪些用户需要被删除，微博数小于后一半的平均值的需要被删除
		public static void avgStatuses() {
			String sql = "select top 57799 * from users order by statuses_count asc";
			try {
				System.out.println(sql);
				Statement stmt = DatabaseDao.getConnection().createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				double sum = 0;
				while (rs.next()) {
					sum += rs.getInt("statuses_count");
				}
				System.out.println(sum / 57799);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		// 删除statusCount和statuses.count不一致的一些users
		public static void deleteUsers() {
			ArrayList<User> users = UserDao.getAll();
			System.out.println("user count:" + users.size());
			Hashtable<String, Integer> userId_statusesCount = StatusDao.getUserIdStatusesCount();
			for (User user : users) {
				int statusesCount_u = user.getStatusesCount();
				int statusesCount_s = userId_statusesCount.get(user.getUserId());
				double closeDegree = (double) (statusesCount_u) / (statusesCount_s);
				if (closeDegree < 0.8 || closeDegree > 1.2) {
					String sql = "delete from users where user_id=" + user.getUserId();
					DatabaseDao.updateData(sql);
				}
			}
		}

}
